<?php
// +----------------------------------------------------------------------
// | 日志模型
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: PostsModel.class.php 122 2011-05-18 09:00:37Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


import('RelationModel');
class PostsModel extends RelationModel {

	protected $_link=array(
		'Category'=>array(
			'mapping_type'=>BELONGS_TO,
			'class_name'=>'Category',
			'foreign_key'=>'category_id',
			'mapping_name'=>'category',
			'mapping_fields'=>'category_title', 
			'as_fields'=>'category_title',
		),
		'Tag'=>array(
			'mapping_type'=>MANY_TO_MANY,
			'class_name'=>'Tag',
			'foreign_key'=>'posts_id',
			'mapping_name'=>'tag',
			'mapping_fields'=>'tag_name', 
			'relation_foreign_key'=>'tag_id',
			'relation_table'=>'nb_posts_tag',
		 ),
		 // 把评论表跟引用表关联起来的数据最准确,但消耗服务器资源
		 // 屏蔽了评论跟引用的关联模型 <volist name="vo.comment" id="c"><a href="__APP__/Comment/listComment/id/{$vo.posts_id}">{$c.comment_num}</a></volist>
		 /*'Comment'=>array(
			'mapping_type'=>HAS_MANY,
			'class_name'=>'Comment',
			'foreign_key'=>'posts_id',
			'mapping_name'=>'comment',
			'mapping_fields'=>'count(comment_id) as comment_num', 
			'condition'=>'comment_status <> 0',
		 ),
		 // 屏蔽了评论跟引用的关联模型 <volist name="vo.trackback" id="c"><a href="__APP__/Trackback/listTrackback/id/{$vo.posts_id}">{$c.trackback_num}</a></volist>
		 'Trackback'=>array(
			'mapping_type'=>HAS_MANY,
			'class_name'=>'Trackback',
			'foreign_key'=>'posts_id',
			'mapping_name'=>'trackback',
			'mapping_fields'=>'count(tb_id) as trackback_num', 
			'condition'=>'tb_visible <> 0',
		 ),*/
	);
	
	public $_validate	=	array(
		array('posts_title','require','{%posts_title_require}'),
		array('posts_url','require','{%posts_url_require}'),
		array('posts_title','','{%posts_title_unique}',0,'unique',3),
		array('posts_url','','{%posts_title_unique}',0,'unique',3),
	);

	public $_auto		=	array(
		array('posts_date','postsDate',1,'callback'),
		array('posts_addtime','time',1,'function'),
		array('posts_url','gdToEncode',3,'callback'),
		array('posts_status','1',1),
	);	
	
	//编码
	protected function gdToEncode() {
		$str = $_POST['posts_url'];
		if (is_gb($str)) {
			return strtolower(urlencode($str));
		} else {
			return strtolower($str);
		}
	}
	
	protected function postsDate() {
		return date('Y-m',time());
	}	
	
  	/**
	 * 插入新日志数据处理标签和Trackback
	 *
	 * @access  public
	 * @param   array     $data    表单数组
	 * @return  array
	 */
    public function insertPostsTag($data) {
		if ($posts_id = $this->add($data)) {
			// Tag处理
			if (!empty($_POST['tag_name'])) {
				$tag_name    = $_POST['tag_name'];
				$tag_name    = str_replace('，', ',', $tag_name);
				$tag_name    = str_replace(',,', ',', $tag_name);
				$arrTag = explode(',',$tag_name);
				if(is_array($arrTag)) {
					$tag = D ('Tag');
					$tag->addTagFromPosts($arrTag,$posts_id);
				}
			}
			// Trackback处理
			if (!empty($_POST['posts_trackback'])) {				
				$url = C('blog_domain')."posts/".$data['posts_url']."/";
				$blog_name = C('blog_name');
				$title = $data['posts_title'];
				$content = msubstr($data['posts_content'],0,252);
				$senddata = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name).'&excerpt='.rawurlencode($content);
				
				$arr = explode('|',$_POST['posts_trackback']);
				foreach ($arr as $key => $v) {
					$pingurl = addslashes(htmlspecialchars($v));			
					$result = trackback_send($pingurl, $senddata);
					if (strpos($result, 'error>0</error')) {
						$tbl = M ('TrackbackLog');
						$d['posts_id'] = $posts_id;
						$d['tbl_addtime'] = gmtime();
						$d['tbl_pingurl'] = $pingurl;
						$tbl_id = $tbl->add($d);
						set_log('send(Trackback, Success, Id: '.$tbl_id.')');
					} else {
						set_log('send(Trackback, Error, Posts_Id: '.$posts_id.')');
					}
				}			
			}
			
			return $posts_id;
		} else {
			return false;
		}	  
	}
	
	/**
	 * 编辑日志数据处理标签
	 *
	 * @access  public
	 * @param   array     $data    表单数组
	 * @return  array
	 */
    public function updataPostsTag($data) {
		if ($this->save($data)) {
			if (!empty($_POST['tag_name'])) {
				$tag_name    = $_POST['tag_name'];
				$tag_name    = str_replace('，', ',', $tag_name);
				$tag_name    = str_replace(',,', ',', $tag_name);
				$arrTag = explode(',',$tag_name);
				if(is_array($arrTag)) {
					$tag = D ('Tag');
					$tag->addTagFromPosts($arrTag,$data['posts_id']);
				}
			}
			// Trackback处理
			if (!empty($_POST['posts_trackback'])&&!empty($_POST['resend_tb'])) {				
				$url = C('blog_domain')."posts/".$data['posts_url']."/";
				$blog_name = C('blog_name');
				$title = $data['posts_title'];
				$content = msubstr($data['posts_content'],0,252);
				$senddata = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name).'&excerpt='.rawurlencode($content);
				
				$arr = explode('|',$_POST['posts_trackback']);
				foreach ($arr as $key => $v) {
					$pingurl = addslashes(htmlspecialchars($v));			
					$result = trackback_send($pingurl, $senddata);
					if (strpos($result, 'error>0</error')) {
						$tbl = M ('TrackbackLog');
						$d['posts_id'] = $data['posts_id'];
						$d['tbl_addtime'] = gmtime();
						$d['tbl_pingurl'] = $pingurl;
						$tbl_id = $tbl->add($d);
						set_log('send(Trackback, Success, Id: '.$tbl_id.')');
					} else {
						set_log('send(Trackback, Error, Posts_Id: '.$data['posts_id'].')');
					}
				}			
			}
			
			return true;
		} else {
			return false;
		}	  
	}
	
}

?>